package blog.service;


import blog.entity.Member;
import blog.entity.Article;
import blog.entity.Comment;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("databaseService") @Transactional
public class DatabaseServiceImpl implements DatabaseService{

	@Autowired private SessionFactory sessionFactory;

	
	//IMPLEMENT FOR MEMBER
	@SuppressWarnings("unchecked")
	@Override
	public List<Member> listMember() {
			return sessionFactory.getCurrentSession().createQuery("FROM Member").list();
	}

	@Override
	public Member findMemberById(Integer idm) {
		return (Member) sessionFactory.getCurrentSession().get(Member.class, idm);
	}

	@Override
	public Integer findIdMemberByMember(String member) {
		StringBuilder query = new StringBuilder();
		query.append("SELECT m.idm FROM Member m ");
		query.append("WHERE m.username = :member");
		return (Integer) sessionFactory.getCurrentSession().
			createQuery(query.toString()).setParameter("member", member).uniqueResult();
	}

	@Override
	public void saveMember(Member member) {
		sessionFactory.getCurrentSession().saveOrUpdate(member);
		
	}

	@Override
	public void deleteMember(Member member) {
		sessionFactory.getCurrentSession().delete(member);
		
	}
	
	
	//IMPLEMENT FOR ARTICLE
	@SuppressWarnings("unchecked")
	@Override
	public List<Article> listArticle() {
		return sessionFactory.getCurrentSession().createQuery("FROM Article").list();
	}

	@Override
	public Article findArticleById(Integer ida) {
		return (Article) sessionFactory.getCurrentSession().get(Article.class, ida);
	}

	@Override
	public Integer findIdArticleByArticle(String article) {
		StringBuilder query = new StringBuilder();
		query.append("SELECT a.ida FROM Article a ");
		query.append("WHERE a.title = :article");
		return (Integer) sessionFactory.getCurrentSession().
			createQuery(query.toString()).setParameter("article", article).uniqueResult();
	}

	@Override
	public void saveArticle(Article article) {
		sessionFactory.getCurrentSession().saveOrUpdate(article);
		
	}

	@Override
	public void deleteArticle(Article article) {
		sessionFactory.getCurrentSession().delete(article);
		
	}
	
	
	//IMPLEMENT FOR COMMENT
	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> listComment() {
		return sessionFactory.getCurrentSession().createQuery("FROM Comment").list();
	}

	@Override
	public Comment findCommentById(Integer idc) {
		return (Comment) sessionFactory.getCurrentSession().get(Comment.class, idc);
	}

	@Override
	public Integer findIdCommentByComment(String comment) {
		StringBuilder query = new StringBuilder();
		query.append("SELECT c.idc FROM Comment c ");
		query.append("WHERE c.sender = :comment");
		return (Integer) sessionFactory.getCurrentSession().
			createQuery(query.toString()).setParameter("comment", comment).uniqueResult();
	}

	@Override
	public void saveComment(Comment comment) {
		sessionFactory.getCurrentSession().saveOrUpdate(comment);
		
	}

	@Override
	public void deleteComment(Comment comment) {
		sessionFactory.getCurrentSession().delete(comment);
		
	}
	
	
	
	
}
